import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/entity/live/live_goods_entity.dart';
import 'package:ball_lion/widgets/loading/loading_indicator.dart';
import 'package:ball_lion/widgets/loading/no_data_widget.dart';
import 'package:ball_lion/module/live/live_goods_logic.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/base/wqs_image.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';
import 'package:ball_lion/widgets/card.dart';

class GoodsBottomSheet extends StatefulWidget {
  const GoodsBottomSheet({super.key, required this.liveId});

  final int? liveId;

  @override
  State<GoodsBottomSheet> createState() => _GoodsBottomSheetState();
}

class _GoodsBottomSheetState extends State<GoodsBottomSheet> {
  late final LiveGoodsLogic logic;

  @override
  void initState() {
    logic = Get.put(LiveGoodsLogic(), tag: "${widget.liveId}");
    logic.liveId = widget.liveId;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LiveGoodsLogic>(
        tag: "${widget.liveId}",
        builder: (logic) {
          return BottomSheet(
              onClosing: () {},
              backgroundColor: Colours.greyF2F3F7,
              constraints: BoxConstraints(
                maxHeight:
                    Get.height - 199 - MediaQuery.of(context).padding.top,
              ),
              builder: (BuildContext context) => CustomScrollView(
                    slivers: [
                      SliverToBoxAdapter(
                        child: Padding(
                          padding: const EdgeInsets.all(12),
                          child: Row(
                            children: [
                              const QsText("商品橱窗",
                                  fontSize: 16, fontWeight: FontWeight.w500),
                              const Spacer(),
                              GestureDetector(
                                onTap: Get.back,
                                child: Image.asset(
                                    Utils.getImgPath('close.png'),
                                    width: 24,
                                    height: 24),
                              )
                            ],
                          ),
                        ),
                      ),
                      if (logic.goods == null)
                        const LoadingIndicator.sliver()
                      else if (logic.goods?.length == 0)
                        const NoDataWidget.sliver()
                      else ...[
                        SliverList.list(
                            children: List.generate(logic.goods?.length ?? 0,
                                (index) => good(index))),
                        SliverToBoxAdapter(
                            child: Container(
                                alignment: Alignment.center,
                                padding:
                                    const EdgeInsets.symmetric(vertical: 16),
                                child: const QsText("暂无更多商品",
                                    fontSize: 12, color: Colours.grey99)))
                      ]
                    ],
                  ));
        });
  }

  Widget good(int index) {
    GoodsEntity entity = logic.goods![index];
    return Padding(
      padding: const EdgeInsets.fromLTRB(8, 0, 8, 8),
      child: Stack(
        alignment: AlignmentDirectional.topStart,
        children: [
          Block(
              onTap: () async {
                Utils.onEvent('liveroom_model_view',
                    params: {'model_name': logic.goods![index].name});
                Utils.doJump(entity.infoHref ?? "");
                Get.back();
              },
              cardStyle:
                  const CardStyle(padding: EdgeInsets.fromLTRB(24, 12, 12, 12)),
              child: Column(
                children: [
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      QsImage(
                        imageUrl: entity.logo,
                        width: 44,
                        height: 44,
                        fit: BoxFit.cover,
                        placeholder: Container(),
                        errorWidget: Styles.placeholderIcon(),
                      ),
                      Container(width: 16),
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            QsText(entity.name ?? "", fontSize: 16),
                            Container(height: 2),
                            QsText(entity.content ?? "", color: Colours.grey99),
                          ],
                        ),
                      )
                    ],
                  ),
                  Row(
                    children: [
                      Container(width: 44 + 16),
                      Container(
                          margin: const EdgeInsets.only(right: 4),
                          padding: const EdgeInsets.symmetric(horizontal: 4),
                          decoration: BoxDecoration(
                              border:
                                  Border.all(width: 0.5, color: Colours.main),
                              borderRadius: BorderRadius.circular(2)),
                          child: const QsText("专属价",
                              needCenter: true,
                              fontSize: 10,
                              color: Colours.main)),
                      if (entity.price == "限时免费")
                        Image.asset(
                            height: 18, Utils.getImgPath("free_for_now.png")),
                      if (entity.price != null && entity.price != "限时免费")
                        Row(
                          children: [
                            QsText(
                              (entity.price?.split("球币").join().trim() ?? "-")
                                  .changeUnit,
                              fontSize: 15,
                              color: Colours.main,
                              fontWeight: FontWeight.w500,
                              needCenter: true,
                            ),
                            QsText(
                              "球币".changeUnit,
                              fontSize: 10,
                              color: Colours.main,
                              fontWeight: FontWeight.w500,
                              needCenter: true,
                            ),
                          ],
                        ),
                      Container(width: 4),
                      if (entity.priceOld != null)
                        QsText("${entity.priceOld ?? "-"}球币".changeUnit,
                            needCenter: true,
                            fontSize: 10,
                            decoration: TextDecoration.lineThrough,
                            color: Colours.grey99),
                      const Spacer(),
                      GestureDetector(
                        onTap: () {
                          Utils.onEvent('liveroom_ai_buy',
                              params: {'model_id': entity.name});
                          Utils.doJump(entity.payHref ?? "");
                        },
                        child: Container(
                          width: 70,
                          height: 30,
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(18),
                              gradient: const LinearGradient(
                                colors: [Color(0xFFFF4789), Color(0xFFFF4667)],
                              )),
                          child: const QsText(
                            "马上抢",
                            fontSize: 13,
                            color: Colours.white,
                          ),
                        ),
                      )
                    ],
                  )
                ],
              )),
          Align(
            alignment: Alignment.topLeft,
            child: Container(
              width: 20,
              height: 20,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Colours.black.withOpacity(0.5),
                borderRadius: const BorderRadius.only(
                    topLeft: Radius.circular(8),
                    bottomRight: Radius.circular(8)),
              ),
              child: QsText("${index + 1}", fontSize: 12, color: Colours.white),
            ),
          )
        ],
      ),
    );
  }
}
